#include "head.h"


bool Control::LastStartFlag = false;

Dot::Dot(QWidget *parent)
    :QWidget(parent)
{
//    setAttribute(Qt::WA_TranslucentBackground, true);
}

Dot::~Dot()
{

}

void Dot::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter paint(this);
    paint.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform, true);
    paint.save();

    QImage image(":/images/images/volumeButton.png");
    paint.drawImage(rect(), image, image.rect());
    paint.restore();
    _mousePos = mapToGlobal(QPoint(0, 0));
}

void Dot::mousePressEvent(QMouseEvent *event)
{
    //防止进度条移动到末尾时，拖动进度条出现无法继续播放所以添加这个判断
    if(SingleInstance::GetInstanca().LoadFile)
    {
        if(event->button() == Qt::LeftButton)
        {
            Control::LastStartFlag = SingleInstance::GetInstanca().StartFlag;
            SingleInstance::GetInstanca().StartFlag = false;//seek时暂停播放，不能在mouseMoveEvent设置因为会不停触发函数导致LastStartFlag被假值覆盖
            _mousePos = event->globalPos();
        }
    }
}

void Dot::mouseMoveEvent(QMouseEvent *event)
{
    if(SingleInstance::GetInstanca().LoadFile)
    {
        if(event->buttons() & Qt::LeftButton)
        {
//            Control::LastStartFlag = SingleInstance::GetInstanca().StartFlag;
//            SingleInstance::GetInstanca().StartFlag = false;
            auto diff = event->globalPos() - _mousePos;
            _mousePos = event->globalPos();
            emit moveDot(diff.x());
        }
    }
}

void Dot::mouseReleaseEvent(QMouseEvent *event)
{
    if(SingleInstance::GetInstanca().LoadFile)
    {        
        auto diff =  event->globalPos() - _mousePos;
        emit EndDot(diff.x());
    }
}

void BarAndDot::upDateProgress()
{
    auto rotia = SingleInstance::GetInstanca().ProgressRotia;
    auto x = rotia * (width() - _dot->width());
    _moveX = x;
    _dot->move(x, _dot->y());

    emit updateTimeStamp(rotia);
    repaint();
}

BarAndDot::BarAndDot(QWidget *parent)
    :QWidget(parent)
{
    setAttribute(Qt::WA_TranslucentBackground, true);
    setAutoFillBackground(true);
    _dot = new Dot(this);
    _dot->setGeometry(0, 0, 32, 48);
//    qDebug() << _dot->pos();
    _dot->setMinimumSize(QSize(32, 48));
    connect(_dot, SIGNAL(moveDot(int)), this, SLOT(acceptMoveDot(int)));
    connect(_dot, SIGNAL(EndDot(int)), this, SLOT(acceptEndDot(int)));


    _ti = new QTimer(this);
    _ti->start(900);
    connect(_ti, SIGNAL(timeout()), this, SLOT(upDateProgress()));
}

BarAndDot::~BarAndDot()
{

}

void BarAndDot::acceptMoveDot(int x)
{
//    qDebug() << _dot->pos();
    if(_dot->x() + x >= 0 && _dot->x() + x <= width() - _dot->width())
    {
        _ti->stop();//移动鼠标开始暂停，避免不停回到原始位置，导致进度条出现闪现
        _dot->move(QPoint(_dot->x() + x, _dot->y()));
        _moveX = _dot->x() + x;
//        qDebug() << _dot->pos();
        double ratio = static_cast<double>(_moveX) / static_cast<double>((width() - 13));
        emit updateTimeStamp(ratio);
        repaint();
    }
}

void BarAndDot::acceptEndDot(int x)
{
    Q_UNUSED(x);
    double ratio = static_cast<double>(_dot->x()) / static_cast<double>((width() - 13));
    _rotia = ratio;
    _ti->start();//移动鼠标松开开始计时
    emit adjustProgress(ratio);
    emit updateTimeStamp(_rotia);
}

void BarAndDot::mouseReleaseEvent(QMouseEvent *event)
{
    if(SingleInstance::GetInstanca().LoadFile)
    {
        auto x = event->pos().x();
        if(x >= 0 &&x <= width() - _dot->width())
        {
            Control::LastStartFlag = SingleInstance::GetInstanca().StartFlag;
            SingleInstance::GetInstanca().StartFlag = false;//暂停播放
            _dot->move(QPoint(x, _dot->y()));
            _moveX = x;
    //        qDebug() << _dot->pos();
            double ratio = static_cast<double>(x) / static_cast<double>((width() - 13));
            _rotia = ratio;
            emit updateTimeStamp(_rotia);
            emit adjustProgress(ratio);
            repaint();
        }
    }
}

void BarAndDot::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter paint(this);
    paint.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform, true);
    paint.save();

    QImage image(":/images/images/volumeBackground.png");
    paint.drawImage(rect(), image, image.rect());
    paint.restore();

    paint.save();
    QImage image2(":/images/images/volumeValue.png");
    auto color = image2.pixel(10,10);
    paint.setPen(color);
    paint.setBrush(QBrush(color));
    paint.drawRoundedRect(13, 18, 14, 14, 2, 2);
    if(_moveX - 13 > 0)
    {
        paint.drawRect(QRect(13 + 14, 18, _moveX - 13, 14));
    }
    paint.restore();
}







Progress::Progress(QWidget *parent)
    :QWidget(parent)
{
    QPalette pal = palette();
    pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));
    setPalette(pal);
    loadWidget();
}

Progress::~Progress()
{

}

void Progress::acceptUpdateTimeStamp(double rotia)
{
    auto tempStamp = rotia * _sumTime;
    auto timeLeft = static_cast<int>(tempStamp);
    auto timeRight = static_cast<int>(_sumTime - tempStamp);
    auto tempLeft = QTime(0, 0).addMSecs(timeLeft);
    auto tempRight = QTime(0, 0).addMSecs(timeRight);
    _labelLeft->setText(tempLeft.toString("hh:mm:ss"));
    _labelRight->setText(tempRight.toString("hh:mm:ss"));
}



void Progress::SetDuration(int64_t time)
{
    _sumTime = time;
    auto temp = QTime(0, 0).addMSecs(time);
    _labelRight->setText(temp.toString("hh:mm:ss"));
}

void Progress::loadWidget()
{
    auto qb = new QHBoxLayout();
    setLayout(qb);
//    qb->setSpacing(0);
//    qb->setContentsMargins(0, 0, 0, 0);
//    qb->setAlignment(Qt::AlignCenter);

    _labelLeft = new QLabel();
    _labelLeft->setText("00:00:00");
    _labelLeft->setFont(QFont(QFont("Arial",18,50)));
    _labelLeft->setStyleSheet("background: transparent; font-size: 18px; color: #ababab");

    qb->addWidget(_labelLeft, 1);
    qb->addStretch(1);

    _barDot = new BarAndDot();
    _barDot->setMinimumSize(QSize(700, 40));
    connect(_barDot, SIGNAL(adjustProgress(double)), this, SIGNAL(adjustProgress(double)));
    qb->addWidget(_barDot, 8);
    qb->addStretch(1);


    _labelRight = new QLabel();
    _labelRight->setText("00:00:00");
    _labelRight->setFont(QFont(QFont("Arial",18,50)));
    _labelRight->setStyleSheet("background: transparent; font-size: 18px; color: #ababab");
    connect(_barDot, SIGNAL(updateTimeStamp(double)), this, SLOT(acceptUpdateTimeStamp(double)));
    qb->addWidget(_labelRight, 1);
    qb->addStretch(4);

}





Control::Control(QWidget *parent)
    :QWidget(parent)
{
//    setStyleSheet("QLabel {background-color: black;}");//设置样式
      loadWidget();
//    setAutoFillBackground(true);
}
Control::~Control()
{

}

void Control::updatePlayStatue()
{
    if(!SingleInstance::GetInstanca().LoadFile)
    {
        return;
    }
    if(SingleInstance::GetInstanca().StartFlag)
    {
        _button->setIcon(QPixmap(":/images/images/playButton.png"));
        SingleInstance::GetInstanca().StartFlag = false;
    }else{
        _button->setIcon(QPixmap(":/images/images/pauseButton.png"));
        SingleInstance::GetInstanca().StartFlag = true;
        SingleInstance::GetInstanca().CondPlayStatues.notify_all();
    }

}

void Control::reset()
{
    _button->setIcon(QPixmap(":/images/images/playButton.png"));
}

void Control::loadWidget()
{
    auto qb = new QHBoxLayout();
    setLayout(qb);
//    qb->setContentsMargins(100, 0, 100, 0);
    qb->setSpacing(0);

//    qb->setContentsMargins(0,)
    auto button = new QPushButton();
    _button = button;
    button->setFlat(true);
    button->setStyleSheet("background-color:transparent;");
    button->setHidden(false);
    button->setMinimumSize(QSize(60, 60));
    button->setIcon(QPixmap(":/images/images/playButton.png"));
    button->setIconSize(QSize(button->width(), button->height()));
    connect(button, SIGNAL(clicked()), this, SLOT(updatePlayStatue()));

    qb->addStretch(4);
    qb->addWidget(button, 2);

    _progress = new Progress();
    connect(_progress, SIGNAL(adjustProgress(double)), this, SIGNAL(adjustProgress(double)));
    qb->addWidget(_progress, 8);
//    connect(_progress, SIGNAL(ad), this, SIGNAL());
    qb->addStretch(4);
}


void Control::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter paint(this);
    paint.setRenderHints(QPainter::SmoothPixmapTransform|QPainter::Antialiasing, true);
    paint.save();
    QImage image(":/images/images/musicPlayerPageBg.png");
    auto color = image.pixel(300, 300);
    paint.setPen(color);
    paint.setBrush(QBrush(color));
    paint.drawRect(rect());
    paint.restore();
}
